<?php
  /*Hour.php
  * Represents a single clock in/out record created by an employee
  * May be modified directly by an authorized manager
  * Employees may create a request to modify
  */
class Hour extends AppModel
{
    var $name = 'Hour';
    var $primaryKey = 'trans_id';
    public $belongsTo = array(
	'Employee' => array(
            'className'     => 'Employee',
            'conditions'    => array('Employee.employee_id' => '$this->employee_id')
        )
    );
    //validation rules
    var $validate = array(
	'proj_id'=> array(
		'rule'=>'numeric',
		'required'=>true
	),
	'clock_id'=> array(
		'required'=>true
	)
    );

  /*
  * Rounding function requested by client
  * Returns the closest quarter hour mark to the supplied date
  */
    public function ClosestQuarter($date){
	
	$seconds = strtotime($date);
	$hours = intval(floor($seconds/3600));
	$secondsRemaining = $seconds%3600;
	$q0 = 0;
	$q1 = 3600/4;
	$q2 = 3600/2;
	$q3 = 3600*(3/4);
	$q4 = 3600;
	$closest;
	$min = 3600;
	if(abs($secondsRemaining-$q0)<$min){
		$closest = $q0;
		$min = abs($secondsRemaining-$q0);
	}
	if(abs($secondsRemaining-$q1)<$min){
		$closest = $q1;
		$min = abs($secondsRemaining-$q1);
	}
	if(abs($secondsRemaining-$q2)<$min){
		$closest = $q2;
		$min = abs($secondsRemaining-$q2);
	}
	if(abs($secondsRemaining-$q3)<$min){
		$closest = $q3;
		$min = abs($secondsRemaining-$q3);
	}
	if(abs($secondsRemaining-$q4)<$min){
		$closest=$q4;
	}
	return($closest+($hours*3600));
	
    }

}
?>
